// 用户相关的中间件
let regCheck = (req,res,next)=>{

  let {username,password,email} = req.body;

  if(!username || !password || !email) return res.send({code:5,msg:'用户名或密码或邮箱不能为空'});

  // 正则校验
  if(!/^[a-zA-Z]\w{2,11}$/.test(username)) return res.send({code:5,msg:'用户名格式错误'});

  if(!/^\w{6,12}$/.test(password)) return res.send({code:5,msg:'密码格式错误'})

  if(!/^\w{6,12}@(qq|163)\.com$/.test(email)) return res.send({code:5,msg:'邮箱格式错误'})

 
  next()
}

// 用户登录数据校验
let loginCheck = (req,res,next)=>{
  
  let {username,password} = req.body;
  
  if(!username || !password ) return res.send({code:5,msg:'用户名或密码'});

  // 正则校验
  if(!/^[a-zA-Z]\w{2,11}$/.test(username)) return res.send({code:5,msg:'用户名格式错误'});

  if(!/^\w{6,12}$/.test(password)) return res.send({code:5,msg:'密码格式错误'})
  
  next()
}


// 用户id校验
let userCheck = (req,res,next)=>{
  
  let {id} = req.params;
  if(!id) return res.send({code:5,msg:'id不能为空'})
  
  
  next()
}


module.exports = {
  regCheck,
  loginCheck,
  userCheck
}